//Write by oshogbo//vx
//23.07.2011
//http://code.google.com/p/streamobs

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class Tray
{ 
  private Window mainWindow;
  private SettingsWindow settingsWindow;

  public Tray(Window getMainWindow, SettingsWindow settingsWindow) 
  {
    this.mainWindow = getMainWindow;
    this.settingsWindow = settingsWindow;

    Image icon;
    if( SystemTray.getSystemTray().getTrayIconSize().height <= 16)
      icon = new ImageIcon("gfx/SCicosmall.jpg").getImage();
    else
      icon = new ImageIcon("gfx/SCico.jpg").getImage();

    //Create menu
    //Create Info button
    PopupMenu menu = new PopupMenu();
    MenuItem subMenu = new MenuItem("Streams");
    subMenu.addActionListener(new ActionListener()
	{
	  public void actionPerformed(ActionEvent e)
	  {
            Tray.this.mainWindow.setVisible(true);
	    Tray.this.settingsWindow.setVisible(false);
          }
	});
    menu.add(subMenu);

    //Create Settings button
    subMenu = new MenuItem("Setting");
    menu.add(subMenu);
    subMenu.addActionListener(new ActionListener()
	{
	  public void actionPerformed(ActionEvent e)
          {
	    Tray.this.settingsWindow.setVisible(true);
	    Tray.this.mainWindow.setVisible(false);
	  }
	});

    //Add separator
    menu.addSeparator();

    //Create exit button
    subMenu = new MenuItem("Exit");
    subMenu.addActionListener(new ActionListener()
	{
	  public void actionPerformed(ActionEvent e)
          {
	    System.exit(0);
	  }
	});
    menu.add(subMenu);

    //Create Try Icon
    TrayIcon tray = new TrayIcon(icon, "Stream Obs", menu);

    //Create Action for Left Mouse Button
    tray.addMouseListener(new MouseAdapter()
	{
	  public void mouseClicked(MouseEvent e)
	  {
	    if( !mainWindow.isVisible() )
	    {
	      mainWindow.setVisible(true);
	      Tray.this.settingsWindow.setVisible(false);
	    }else
	    {
	      mainWindow.setVisible(false);
	    }
	  }
	});

    //Try to add TrayIcon
    try
    {
      SystemTray.getSystemTray().add(tray);
    }catch(AWTException e)
    {
      System.err.print("Cannot add Tray Icon");
      e.printStackTrace();
    }
  }
}

